<?php
require __dir__ . '/bootstrap.php';
$em = require __dir__ . '/example/em.php';

if (isset($_POST['forgot_email'])) {
    $email = urldecode($_POST['forgot_email']);
} else {
    exit();
}

$name = $_POST['forgot_login'];
$email = $_POST['forgot_email'];
if (!$em->getRepository("Entity\User")->getUserByName($name)) {
    // no user ....
    echo '{ "message": "Такой пользователь не зарегистрирован в системе!", "type": "st-error" }';
    exit();
} else {
    $user = $em->getRepository("Entity\User")->getUserByName($name);

    if ($user->getEmail() != $email) {
        echo '{ "message": "Такой e-mail не зарегистрирован в системе!", "type": "st-error" }';
        exit();
    } else {
        $new_password = $user->getName() . rand(1000000, 9999999);
        $user->setPassword(md5($new_password));

        require_once('lib/PHPMailer_5.2.4/class.phpmailer.php');

        $mail = new PHPMailer(); // defaults to using php "mail()"

        if (isset($_POST['forgot_email'])) {
            //$body             = file_get_contents('contents.html');
            //$body             = preg_replace("[\]/i",'',$body);
            $body = 'Ваш новый пароль: ' . $new_password;
        }

        $mail->AddReplyTo("partners@smart-sim.ru", "");

        $mail->SetFrom('partners@smart-sim.ru', '');

        $mail->AddReplyTo("partners@smart-sim.ru", "");

        //Кому
        $address = $email;

        //Кому
        $mail->AddAddress($address, $name);
        if (isset($_POST['forgot_email'])) {
            $mail->Subject = "Восстановления пароля на Agent SmartSim";
        }

        //$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
        $mail->CharSet = 'UTF-8';
        $mail->ContentType = 'text/plain';
        $mail->IsHTML(false);
        $mail->Body = $body;
        //$mail->AddAttachment("lib/PHPMailer_5.2.4/examples/images/phpmailer.gif");      // attachment
        //$mail->AddAttachment("lib/PHPMailer_5.2.4/examples/images/phpmailer_mini.gif"); // attachment

        if (!$mail->Send()) {
            echo '{ "message": "Ошибка! ' . $mail->ErrorInfo . '", "type": "st-error" }';
        } else {
            if (isset($_POST['forgot_email'])) {
                $em->flush();
                echo '{ "message": "Новый пароль выслан на ваш e-mail!", "type": "st-success" }';
            }
        }
    }
}

exit();

//old

echo '<pre>';
var_dump($user->getName());
var_dump($user->getEmail());
echo '</pre>';


if (!$em->getRepository("Entity\User")->isSetEmail($email)) {
    // no e-mail ....
    echo '{ "message": "Такой e-mail не зарегистрирован в системе!", "type": "st-error" }';
    exit();
} else {


    $user = $em->getRepository("Entity\User")->isSetEmail($email);
    //var_dump($user);


    $new_password = rand(1000000, 9999999);
    $name = $user->getName();
    print_r($name);
    exit();
    $user->setPassword(md5($new_password));
    $em->flush();

    require_once('lib/PHPMailer_5.2.4/class.phpmailer.php');

    $mail = new PHPMailer(); // defaults to using php "mail()"

    if (isset($_POST['forgot_email'])) {
        //$body             = file_get_contents('contents.html');
        //$body             = preg_replace("[\]/i",'',$body);
        $body = 'Ваш новый пароль: ' . $new_password;
    }

    $mail->AddReplyTo("partners@smart-sim.ru", "");

    $mail->SetFrom('partners@smart-sim.ru', '');

    $mail->AddReplyTo("partners@smart-sim.ru", "");

    //Кому
    $address = $email;

    //Кому
    $mail->AddAddress($address, $name);
    if (isset($_POST['forgot_email'])) {
        $mail->Subject = "Восстановления пароля на Agent SmartSim";
    }

    //$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
    $mail->CharSet = 'UTF-8';
    $mail->ContentType = 'text/plain';
    $mail->IsHTML(false);
    $mail->Body = $body;
    //$mail->AddAttachment("lib/PHPMailer_5.2.4/examples/images/phpmailer.gif");      // attachment
    //$mail->AddAttachment("lib/PHPMailer_5.2.4/examples/images/phpmailer_mini.gif"); // attachment

    if (!$mail->Send()) {
        echo '{ "message": "Ошибка! ' . $mail->ErrorInfo . '", "type": "st-error" }';
    } else {
        if (isset($_POST['forgot_email'])) {
            echo '{ "message": "Новый пароль выслан на ваш e-mail!", "type": "st-success" }';
        }
    }
}
?>